#include "mex.h"
#include "cc.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	int nclusters;
	double *inMatrix;
	unsigned int size_x, size_y;
	double *outMatrix;
	float *in, *out;

	if (nrhs != 2) {
		mexErrMsgTxt("Two inputs required.");
	}

	if (nlhs > 1) {
		mexErrMsgTxt("Cannot have more than one input.");
	}

	if (mxGetN(prhs[0]) != 3) {
		mexErrMsgTxt("Input not in the right form.");
	}

	inMatrix = mxGetPr(prhs[0]);
	size_x = mxGetN(prhs[0]);
	size_y = mxGetM(prhs[0]);
	nclusters = mxGetScalar(prhs[1]);

	out = mxMalloc(sizeof(float)*(size_y+1));
	in = mxMalloc(sizeof(float)*size_x*size_y);
	plhs[0] = mxCreateDoubleMatrix(size_y+1, 1, mxREAL);
	outMatrix = mxGetPr(plhs[0]);

	double2float(in, inMatrix, size_x, size_y, 1);

	clustertree(out, in, size_y+1, nclusters);

	float2double(outMatrix, out, 1, size_y+1, 0);

	mxFree(in);
	mxFree(out);
}
